<!--获取数据-->
<script setup>
import api from '../api/User_api.js'
import { computed, ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import lodash from "lodash";

const tableData = ref({
  data: []
});
const editWinVisible = ref(false);
const addWinVisible = ref(false);
const total = ref(50);      // 总数据条数

//查询数据
const serchObj = ref({
  current:1,
  limit:10,
  total:0,
  userNickname:'',
  userNumber:'',
  userPassword:'',
  photo:'',
  role:'',
  isdel: ''
})

//要修改或新增的数据
const obj = ref({});
const addForm = ref({}); // 新增一个单独的对象用于添加表单

//页面刷新函数
const sear = async()=>{
  const res = await api.page(serchObj.value);
  tableData.value.data = res.data;
  tableData.value.count = res.count;
  total.value=res.count;
  console.log("切换到页码:");
}
//vue组件的生命周期
onMounted(async ()=>{
  sear();
});

const search = ref('')

const filteredData = computed(() =>
    tableData.value.data.filter(
        (item) =>
            !search.value ||
            (item.name && item.name.toLowerCase().includes(search.value.toLowerCase()))
    )
)

const handleEdit = (index, row) => {
  obj.value=lodash.clone(row);
  editWinVisible.value = true;
  console.log(index, row)
}
const handlePageChange = (newPage) => {
  serchObj.value.current = newPage; // 确保查询参数更新
  sear(); // 重新加载数据
};


const handleAdd = () => {
  addForm.value = {
    role: 'user',
    isdel: 1
  };
  addWinVisible.value = true;
}

const editCancel=()=>{
  obj.value={};
  editWinVisible.value=false;
}

const editCommit=async()=>{
  await api.edit(obj.value);
  sear();
  editWinVisible.value=false;
  ElMessage.success('修改成功');
}

const addCancel=()=>{
  addForm.value={};
  addWinVisible.value=false;
}

const addCommit = async () => {
  await api.add(addForm.value);
  sear();
  addWinVisible.value = false;
  ElMessage.success('添加成功');
}

const handlerSearch=()=>{
  serchObj.value.current=1;
  sear();
}

const handleDel = (index, row) => {
  ElMessageBox.confirm(
      '是否确定注销该用户',
      '注意！',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async() => {
        await api.del(row.id);
        serchObj.value.current=1;
        sear();
        ElMessage.success('注销成功');
      })
      .catch(() => {
        ElMessage.success('注销失败');

      })
}
const handleDelete = (index, row) => {
  ElMessageBox.confirm(
      '是否确定删除该用户，该信息将不存在',
      '警告！',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
      }
  )
      .then(async() => {
        await api.delete(row.id);
        serchObj.value.current=1;
        sear();
        ElMessage.success('删除成功');
      })
      .catch(() => {
        ElMessage.success('删除失败');
      })
}
</script>

<!--显示数据-->
<template>
  <el-form :inline="true" :model="serchObj" class="demo-form-inline" style="margin-top: 10px">
    <el-form-item label="用户昵称">
      <el-input v-model="serchObj.userNickname" placeholder="请输入用户昵称" clearable/>
    </el-form-item>
    <el-form-item label="用户账号">
      <el-input v-model="serchObj.userNumber" placeholder="请输入用户账号" clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handlerSearch">查询</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="filteredData" style="width: 100%">
    <el-table-column label="用户昵称" prop="userNickname"/>
    <el-table-column label="用户账号" prop="userNumber" />
<!--    <el-table-column label="用户密码" prop="userPassword" />-->
    <el-table-column label="用户权限" prop="role" />
    <el-table-column label="账号状态（1正常 0注销）" prop="isdel" />
    <el-table-column align="right">
      <template #header>
        <el-button type="primary" @click="handleAdd()">添加</el-button>
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button size="small" type="danger" @click="handleDel(scope.$index, scope.row)">注销</el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      v-model:current-page="serchObj.current"
      :page-size="serchObj.limit"
      :total="total"
      :hide-on-single-page="true"
      layout="prev, pager, next"
      @current-change="handlePageChange"
  />
  <!-- 编辑对话框 -->
  <el-dialog v-model="editWinVisible" title="编辑" width="500">
    <el-form :model="obj">
      <el-form-item label="用户账号" :label-width="100">
        <el-input v-model="obj.userNumber" autocomplete="off" readonly/>
      </el-form-item>
<!--      <el-form-item label="用户密码" :label-width="100">-->
<!--        <el-input v-model="obj.userPassword" autocomplete="off" readonly/>-->
<!--      </el-form-item>-->
      <el-form-item label="用户权限" :label-width="100">
        <el-select placeholder="更改权限" v-model="obj.role">
          <el-option label="用户" value="user"></el-option>
          <el-option label="管理员" value="administrator"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="editCancel">取消</el-button>
        <el-button type="primary" @click="editCommit">提交</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 添加对话框 -->
  <el-dialog v-model="addWinVisible" title="添加" width="500">
    <el-form :model="addForm">
      <el-form-item label="用户昵称" :label-width="100">
        <el-input v-model="addForm.userNickname" autocomplete="off" />
      </el-form-item>
      <el-form-item label="用户账号" :label-width="100">
        <el-input v-model="addForm.userNumber" autocomplete="off" />
      </el-form-item>
      <el-form-item label="用户密码" :label-width="100">
        <el-input v-model="addForm.userPassword" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addCancel">取消</el-button>
        <el-button type="primary" @click="addCommit">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>